trigger changeplanDocStatus on Plan_Document_Inquiry__c (after update) {

    List<ID> salesContractIdList = new list<ID>();
    List<String> pdInquiryStatusList = new List<String>();
 
    for(Plan_Document_Inquiry__c[] pdInquiries: [Select Name, InquiryStatus__c,Plan_Document__c,Description__c,Id,Url__c from Plan_Document_Inquiry__c where Id in:Trigger.new])
    {
        for(Plan_Document_Inquiry__c pdInquiry : pdInquiries)
        {
             salesContractIdList.add(pdInquiry.Plan_Document__c);
             
             if(pdInquiry.InquiryStatus__c=='Inquiry Approved')
                 pdInquiryStatusList.add('Inquiry Approved');
             else
                 pdInquiryStatusList.add('Inquiry Rejected');
             
             if(pdInquiries.size() == salesContractIdList.size())
             {
                 Sales_Contract__c[] contract=[Select Id,Status__c, UserID__c, OwnerId from Sales_Contract__c sc where sc.Id in :salesContractIdList];
                 for(Integer z=0; z<contract.size(); z++)
                 {
                     if(pdInquiryStatusList.get(z).equals('Inquiry Approved'))
                        contract[z].Status__c='Inquiry Approved';
                     else
                        contract[z].Status__c='Inquiry Rejected';
                        
                 }//end of for(Integer z=0; z<pdInquiries.size(); z++)
 
                 update contract;
                 
                 salesContractIdList.clear();
                 pdInquiryStatusList.clear();
             }
        }
    }
}

/*
trigger changeplanDocStatus on Plan_Document_Inquiry__c (after update) {
Map<Id, User> mapList = new Map<Id,User>();
List<ID> salesContractIdList = new list<ID>();
List<ID> pdInquiryIdList = new List<ID>();
List<String> pdInquiryNameList = new List<String>();
List<String> pdInquiryStatusList = new List<String>();
 
for(User[] users : [Select FirstName, Email from User])
{
    for(User u:users)
    {
        mapList.put(u.Id, u);
    }
}
 
      for(Plan_Document_Inquiry__c[] pdInquiries: [Select Name, InquiryStatus__c,Plan_Document__c,Description__c,Id,Url__c from Plan_Document_Inquiry__c where Id in:Trigger.new])
      {
          for(Plan_Document_Inquiry__c pdInquiry : pdInquiries)
          {
 
             salesContractIdList.add(pdInquiry.Plan_Document__c);
             pdInquiryIdList.add(pdInquiry.Id);
             pdInquiryNameList.add(pdInquiry.Name);
             
             if(pdInquiry.InquiryStatus__c=='Inquiry Approved')
                 pdInquiryStatusList.add('Inquiry Approved');
             else
                 pdInquiryStatusList.add('Inquiry Rejected');
             
             if(pdInquiries.size() == salesContractIdList.size())
             {
                 Sales_Contract__c[] contract=[Select Id,Status__c, UserID__c from Sales_Contract__c sc where sc.Id in :salesContractIdList];
                 Messaging.SingleEmailMessage[] mail = new Messaging.SingleEmailMessage[contract.size()];                                              
                 Integer k = 0;      
                 for(Integer z=0; z<contract.size(); z++)
                 {
                     if(pdInquiryStatusList.get(z).equals('Inquiry Approved'))
                        contract[z].Status__c='Inquiry Approved';
                     else
                        contract[z].Status__c='Inquiry Rejected';
                     
                     User u = mapList.get(contract[z].UserID__c);
                     
                     String firstName = u.FirstName;
        
                     // Strings to hold the email addresses to which you are sending the email.
                     String[] toAddresses = new String[] {u.Email}; 
                  
                     mail[k] = new Messaging.SingleEmailMessage();
                     // Assign the addresses for the To and CC lists to the mail object.
                     mail[k].setToAddresses(toAddresses);
                
                     // Specify the name used as the display name.
                     mail[k].setSenderDisplayName('Salesforce OnDemand');
                 
                     // Specify the subject line for your email address.
                     mail[k].setSubject(''+pdInquiryNameList.get(z)+'');
                
                     // Set to True if you want to BCC yourself on the email.
                     mail[k].setBccSender(false);
                    
                
                     // Optionally append the salesforce.com email signature to the email.
                     // The email address of the user executing the Apex Code will be used.
                     mail[k].setUseSignature(false);
                    
         
                     // Specify the text content of the email.
                     mail[k].setPlainTextBody('Your Plan Document Inquiry is approved.');
                     
                     mail[k].setHtmlBody('Dear <b>'+u.FirstName+'</b>,<br/><br/>Your Plan Document Inquiry is processed.<br/><a href="'+pdInquiry.Url__c+'/'+(pdInquiryIdList.get(z))+'">Click here</a> to view.');
                     k++;    
                     
                 }//end of for(Integer z=0; z<pdInquiries.size(); z++)
 
                 update contract;
 
                 // Send the email you have created.
                 Messaging.sendEmail(mail);
                 salesContractIdList.clear();
                 mail.clear();
                 pdInquiryIdList.clear();
                 pdInquiryStatusList.clear();
                 pdInquiryNameList.clear();                 
             }
             
          }
 
    }
}
*/


/*trigger changeplanDocStatus on Plan_Document_Inquiry__c (after update) {
Map<Id, User> mapList = new Map<Id,User>();
for(User[] users : [Select FirstName, Email from User])
{
    for(User u:users)
    {
        mapList.put(u.Id, u);
    }
}

if(Trigger.isUpdate){
      for(Plan_Document_Inquiry__c[] pdInquiries: [Select Name, InquiryStatus__c,Plan_Document__c,Description__c,Id,Url__c from Plan_Document_Inquiry__c where Id in:Trigger.new])
      {
          Messaging.SingleEmailMessage[] mail = new Messaging.SingleEmailMessage[pdInquiries.size()];                                              
          Integer k = 0;      
          for(Plan_Document_Inquiry__c pdInquiry : pdInquiries)
          {

             if(pdInquiry.InquiryStatus__c=='Inquiry Approved')
             {
                 Sales_Contract__c contract=[Select Id,Status__c, UserID__c from Sales_Contract__c sc where sc.Id=:pdInquiry.Plan_Document__c];
                 contract.Status__c='Inquiry Approved';
                 update contract;
                
                User u = mapList.get(contract.UserID__c);
                
                String firstName = u.FirstName;
    
                // Strings to hold the email addresses to which you are sending the email.
                String[] toAddresses = new String[] {u.Email}; 
              
                mail[k] = new Messaging.SingleEmailMessage();
                // Assign the addresses for the To and CC lists to the mail object.
                mail[k].setToAddresses(toAddresses);
            
                // Specify the address used when the recipients reply to the email. 
//                        mail[k].setReplyTo('prashanth.pittala@sierraatlantic.com');
            
                // Specify the name used as the display name.
                mail[k].setSenderDisplayName('Salesforce OnDemand');
            
                // Specify the subject line for your email address.
                mail[k].setSubject(''+pdInquiry.Name+'');
            
                // Set to True if you want to BCC yourself on the email.
                mail[k].setBccSender(false);
                
            
                // Optionally append the salesforce.com email signature to the email.
                // The email address of the user executing the Apex Code will be used.
                mail[k].setUseSignature(false);
                
     
                // Specify the text content of the email.
                mail[k].setPlainTextBody('Your Plan Document Inquiry is approved.');
                
                mail[k].setHtmlBody('Dear <b>'+u.FirstName+'</b>,<br/><br/>Your Plan Document Inquiry is processed.<br/><a href="'+pdInquiry.Url__c+'/'+pdInquiry.Id+'">Click here</a> to view.');
                k++;
    
             }
             if(pdInquiry.InquiryStatus__c=='Inquiry Rejected'){
                 Sales_Contract__c contract=[Select Id, Status__c, UserID__c from Sales_Contract__c sc where sc.Id=:pdInquiry.Plan_Document__c];
                 contract.Status__c='Inquiry Rejected';
                 update contract;
    
                User u = mapList.get(contract.UserID__c);
  
                    String firstName = u.FirstName;
        
                    // Strings to hold the email addresses to which you are sending the email.
                    String[] toAddresses = new String[] {u.Email}; 
                  
                    mail[k] = new Messaging.SingleEmailMessage();
                
                    // Assign the addresses for the To and CC lists to the mail object.
                    mail[k].setToAddresses(toAddresses);
                
                    // Specify the address used when the recipients reply to the email. 
//                        mail[k].setReplyTo('prashanth.pittala@sierraatlantic.com');
                
                    // Specify the name used as the display name.
                    mail[k].setSenderDisplayName('Salesforce OnDemand');
                
                    // Specify the subject line for your email address.
                    mail[k].setSubject(''+pdInquiry.Name+'');
                
                    // Set to True if you want to BCC yourself on the email.
                    mail[k].setBccSender(false);
                    
                
                    // Optionally append the salesforce.com email signature to the email.
                    // The email address of the user executing the Apex Code will be used.
                    mail[k].setUseSignature(false);
                    
         
                    // Specify the text content of the email.
                    mail[k].setPlainTextBody('Your Plan Document Inquiry is rejected.');
                    
                    mail[k].setHtmlBody('Dear <b>'+u.FirstName+'</b>,<br/><br/>Your Plan Document Inquiry is processed.<br/><a href="'+pdInquiry.Url__c+'/'+pdInquiry.Id+'">Click here</a> to view.');
                    k++;
            
             }
    
        }
        // Send the email you have created.
        Messaging.sendEmail(mail);
    }

    
    
    
}
}
*/